package com.cmict.data.system.controller.admin.portal;

import com.cmict.data.framework.common.pojo.CommonResult;
import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.framework.datapermission.core.annotation.DataPermission;
import com.cmict.data.framework.excel.core.util.ExcelUtils;
import com.cmict.data.framework.operatelog.core.annotations.OperateLog;
import com.cmict.data.form.api.task.dto.SysFormDTO;
import com.cmict.data.framework.tenant.core.aop.TenantIgnore;
import com.cmict.data.system.controller.admin.portal.vo.*;
import com.cmict.data.system.controller.admin.portal.vo.*;
import com.cmict.data.system.convert.portal.PortalConvert;
import com.cmict.data.system.dal.dataobject.portal.PortalDO;
import com.cmict.data.system.service.portal.PortalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.cmict.data.framework.common.pojo.CommonResult.success;
import static com.cmict.data.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
import static com.cmict.data.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Api(tags = "管理后台 - 门户")
@RestController
@RequestMapping("/sys/portal")
@Validated
public class PortalController {

    @Resource
    private PortalService portalService;

    @PostMapping("/create")
    @ApiOperation("创建门户")
    @PreAuthorize("@ss.hasPermission('sys:portal:create')")
    public CommonResult<Long> createPortal(@Valid @RequestBody PortalCreateReqVO createReqVO) {
        return success(portalService.createPortal(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新门户")
    @PreAuthorize("@ss.hasPermission('sys:portal:update')")
    public CommonResult<Boolean> updatePortal(@Valid @RequestBody PortalUpdateReqVO updateReqVO) {
        portalService.updatePortal(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除门户")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('sys:portal:delete')")
    public CommonResult<Boolean> deletePortal(@RequestParam("id") Long id) {
        portalService.deletePortal(id);
        return success(true);
    }

    @DeleteMapping("/deleteNoMenu")
    @ApiOperation("删除门户-不关心menu")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    public CommonResult<Boolean> deletePortalNoMenu(@RequestParam("id") Long id) {
        portalService.deletePortalNoMenu(id);
        return success(true);
    }

    @DeleteMapping("/batchDelete")
    @ApiOperation("批量删除门户")
    @PreAuthorize("@ss.hasPermission('sys:portal:delete')")
    public CommonResult<Boolean> batchDeletePortal(@RequestBody Collection<Long> ids) {
        portalService.batchDeletePortal(ids);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得门户")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('sys:portal:query')")
    public CommonResult<PortalRespVO> getPortal(@RequestParam("id") Long id) {
        PortalDO portal = portalService.getPortal(id);
        return success(PortalConvert.INSTANCE.convert(portal));
    }

    @GetMapping("/getMoveOne")
    @ApiOperation("获得门户-手机侧")
    @TenantIgnore
    public CommonResult<Map<Object, Object>> getMoveOne(@RequestParam("id") String id, int isReference) {
        Map<Object, Object> portal = portalService.getMoveOne(id,isReference);
        return success(portal);
    }

    @GetMapping("/list")
    @ApiOperation("获得门户列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('sys:portal:query')")
    public CommonResult<List<PortalRespVO>> getPortalList(@RequestParam("ids") Collection<Long> ids) {
        List<PortalDO> list = portalService.getPortalList(ids);
        return success(PortalConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得门户分页")
    @PreAuthorize("@ss.hasPermission('sys:portal:query')")
    public CommonResult<PageResult<PortalRespVO>> getPortalPage(@Valid PortalPageReqVO pageVO) {
        PageResult<PortalDO> pageResult = portalService.getPortalPage(pageVO);
        return success(PortalConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出门户 Excel")
    @PreAuthorize("@ss.hasPermission('sys:portal:export')")
    @OperateLog(type = EXPORT)
    public void exportPortalExcel(@Valid PortalExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PortalDO> list = portalService.getPortalList(exportReqVO);
        // 导出 Excel
        List<PortalExcelVO> datas = PortalConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "门户.xls", "数据", PortalExcelVO.class, datas);
    }
    @ApiOperation(value = "获取业务库所有表名")
    @GetMapping(value = "/getTableNameList")
    @PreAuthorize("@ss.hasPermission('sys:portal:query')")
    public CommonResult<List<SysFormDTO>> getTableNameList() throws Exception {
        return success(portalService.getTableNameList());
    }

    /**
     * 门户携带菜单列表分页查询
     * @param reqVO 表单表
     * @return
     */
    @ApiOperation(value = "门户携带菜单列表分页查询", notes = "门户携带菜单列表分页查询")
    @GetMapping("/pageRelease" )
    @DataPermission(enable = true)
    @PreAuthorize("@ss.hasPermission('sys:portal:query')")
    public CommonResult<PageResult<PortalRespVO>> getPortalPageRelease(PortalPageReqVO reqVO) {
        return success(portalService.getPortalPageRelease(reqVO));
    }

    @ApiOperation(value = "获取表中的所有字段")
    @GetMapping(value = "/getFieldNameList")
    @PreAuthorize("@ss.hasPermission('sys:portal:query')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tableName", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = String.class),
            @ApiImplicitParam(name = "type", value = "编号列表", example = "1024,2048", dataTypeClass = String.class),
    })
    public CommonResult<List<Map<String,Object>>> getFieldNameList(@RequestParam("tableName") String tableName, String type) throws Exception {
        return success(portalService.getFieldNameList(tableName,type));
    }

    @ApiOperation(value = "获取表中字段数据")
    @GetMapping(value = "/getFieldData")
    @PreAuthorize("@ss.hasPermission('sys:portal:query')")
    @DataPermission(enable = true)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fieldName", value = "字段名称", required = true, example = "1024,2048", dataTypeClass = String.class),
            @ApiImplicitParam(name = "tableName", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = String.class),
            @ApiImplicitParam(name = "type", value = "编号列表", example = "1024,2048", dataTypeClass = String.class),
    })
    public CommonResult getFieldData(@RequestParam("fieldName")String  fieldName, @RequestParam("tableName")String tableName, String type) throws Exception {
        Object data = portalService.getFieldData(fieldName,tableName,type);
        return success(data);
    }

    @ApiOperation(value = "获取图表数据")
    @GetMapping(value = "/getChartData")
    @DataPermission(enable = true)
    @PreAuthorize("@ss.hasPermission('sys:portal:query')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fieldNameX", value = "X字段名称", required = true, example = "1024", dataTypeClass = String.class),
            @ApiImplicitParam(name = "fieldNameY", value = "X字段名称", required = true, example = "2048", dataTypeClass = String.class),
            @ApiImplicitParam(name = "tableName", value = "编号列表", required = true, example = "2048", dataTypeClass = String.class),
            @ApiImplicitParam(name = "type", value = "编号列表",required = false, example = "2048", dataTypeClass = String.class),
    })
    public CommonResult getChartData(@RequestParam("fieldNameX")String  fieldNameX, @RequestParam("fieldNameY")String  fieldNameY,
                                     @RequestParam("tableName")String tableName, String type) throws Exception {
        Object data = portalService.getChartData(fieldNameX,fieldNameY,tableName,type);
        return success(data);
    }
    /**
     *  求和 平均数 最大值 最小值
     * @param ""
     * @param
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "函数计算")
    @GetMapping(value = "/getFunData")
    @DataPermission(enable = true)
    @PreAuthorize("@ss.hasPermission('sys:portal:query')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fun", value = "函数", required = true, example = "1024", dataTypeClass = String.class),
            @ApiImplicitParam(name = "fieldName", value = "字段名称", required = true, example = "1024", dataTypeClass = String.class),
            @ApiImplicitParam(name = "tableName", value = "编号列表", required = true, example = "2048", dataTypeClass = String.class)
    })
    public CommonResult getFunData(@RequestParam("fun") String fun , @RequestParam("fieldName")String  fieldName, @RequestParam("tableName")String tableName) throws Exception {
        BigDecimal data = portalService.getFunData(fun,fieldName,tableName);
        return success(data);
    }

    /**
     *  用户表单列表
     * @return
     */
    @ApiOperation(value = "用户表单列表")
    @GetMapping(value = "/getUserForm")
    public CommonResult getUserFormList() {
        Set<SysFormDTO> userFormList= portalService.getUserFormList(getLoginUserId());
        return success(userFormList);
    }
}
